import axios from "axios"; 
import { getToken } from "./loginInfo"

const request = axios.create({
    baseURL: process.env.VUE_APP_API,
    timeout:5000
});

request.interceptors.request.use( 
    (config) => {
        const token = getToken();
        if(token)
            //  添加请求头
            config.headers.Authorization = `Bearer ${token}`;
        return config;
    },
    (error) => {
        return Promise.reject(error);
    }
);

request.interceptors.response.use(
    (response) => {
        const res = response.data;
        if(res.code === 200)
            return res;
    },
    (error) => {
        /*
        let msg = "";

        if(error.response.data.msg){
            msg = error.response.data.msg;
        }else{
            const status = error.response.status;
            switch(status){
                case 400:
                    msg = "请求失败";
                    break;
                case 401:
                    msg = "未登录或身份过期";
                    break;
                case 403:
                    msg = "权限不足";
                    break;
                case 404:
                    msg = "请求地址错误";
                    break;
                case 500:
                    msg = "服务器出现问题";
                    break;
                default:
                    msg = "网络出现问题";
                    break;
            }
        }
        */
        return Promise.reject(error);
    }
);

export default request;